package com.zztl.infiniteBand.presenter


import android.content.Context
import android.text.TextUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BasePresenter
import com.zztl.infiniteBand.model.BindedDeviceBean
import com.zztl.infiniteBand.model.LoginBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.FilterSubscriber2
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.requestBean.LoginRequest
import com.zztl.infiniteBand.ui.view.mvpview.LoginView
import com.zztl.infiniteBand.util.UiUtils
import com.zztl.infiniteBand.util.toast


/**
 * @desc
 * @auth ${user}
 * @time 2018/6/4  14:25
 */
class LoginPresenter : BasePresenter<LoginView>() {
    fun check(phone: String, password: String): Boolean {

        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
            UiUtils.context.toast(UiUtils.getString(R.string.手机号或密码不能为空))
            return false
        }
        return true
    }

    fun login(loginRequest: LoginRequest, context: Context) {
        addSubscribe(RetorfitServiceManger.observe(RetorfitServiceManger.instance!!.apiService.login(DataConverter.instance.toJson(loginRequest)))
                .subscribe(object : FilterSubscriber<LoginBean>(context) {

                    override fun success(loginBean: LoginBean) {
                        view?.loginSuccess(loginBean)
                    }

                    override fun fail() {
                        super.fail()
                        view?.loginFail()
                    }
                }))
    }


    fun getBindDevice(context: Context) {
        addSubscribe(RetorfitServiceManger.observe(RetorfitServiceManger.instance!!.apiService.getBindDeviceList(""))
                .subscribe(object : FilterSubscriber2<BindedDeviceBean>(context) {


                    override fun success(bean: BindedDeviceBean) {
                        view!!.getBindDeviceList(bean)
                    }

                    override fun fail(msg: String?) {
                        view!!.getBindDeviceListFail()
                    }
                }))
    }


}
